﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.ComponentModel;
using XCI.Core;
using XCI.Helper;

namespace XCI.Windows
{
    /// <summary>
    /// 数据字典多选控件
    /// </summary>
    [System.ComponentModel.DesignerCategory("Code")]
    public class DicCheckedListBox : CheckedListBoxControlx
    {
        static DicCheckedListBox()
        {

        }

        public DicCheckedListBox()
        {
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string ValueMember => "Value";
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string DisplayMember => "Name";

        /// <summary>
        /// 字典编码
        /// </summary>
        [Category("XCI"), DefaultValue(null), Description("字典编码")]
        public string DicCode { get; set; }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
            if (string.IsNullOrEmpty(DicCode)) return;
            this.CheckOnClick = true;
            this.MultiColumn = true;
            this.SelectionMode = System.Windows.Forms.SelectionMode.None;
            this.ShowFocusRect = false;

            LoadDic(DicCode);
        }

        public void LoadDic(string code)
        {
            var dic = App.Handler.GetDic(code);
            DataSource = dic.Items;
        }
    }
}